package com.supplychain.scm.core.common.validator.wechat;

import com.supplychain.scm.core.common.validator.ValidationUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName WeChatValidationImpl
 * @Description 微信号码校验实现
 * @createTime 12:10:00 2022/11/08
 */
public class WeChatValidationImpl implements ConstraintValidator<WeChat,String> {

	private boolean required = false;

	@Override
	public void initialize(WeChat phoneValidation) {
		required = phoneValidation.required();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
		if(required){
			return ValidationUtils.isWechat(value);
		}else{
			if(StringUtils.isBlank(value)){
				return true;
			}else{
				return ValidationUtils.isWechat(value);
			}
		}
	}
}
